<template>
  <div>
    <el-form ref="form" :model="admin" label-width="80px" size="mini">
      <el-form-item label="新密码">
        <el-input v-model="admin.password" type="password" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" size="mini" @click="onSubmit">保存</el-button>
        <el-button size="mini" @click="CloseForm">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import adminApi from '@/api/admin'
import md5 from 'js-md5'
export default {
  props: {
    admin: {
      type: Object,
      default: null
    }
  },
  methods: {
    onSubmit() {
      this.admin.password = md5(this.admin.password)
      adminApi.updatePassword(this.admin).then(resp => {
        this.admin.password = null
        this.$message.success(resp.msg)
        this.logout()
      })
    },
    CloseForm() {
      this.$emit('closeUpdateForm')
    },
    async logout() {
      await this.$store.dispatch('user/logout')
      this.$router.push(`/login?redirect=${this.$route.fullPath}`)
    }
  }
}
</script>
